package com.library.action;

import java.io.PrintWriter;
import java.util.List;

import com.library.common.SPageBean;
import com.library.model.Bookinfo;
import com.library.service.AdminBookService;

public class AdminBookAction extends BaseAction{
	private AdminBookService adminBookService;
	private Bookinfo book;
	private List<Bookinfo> books;
	
	public AdminBookService getAdminBookService() {
		return adminBookService;
	}

	public void setAdminBookService(AdminBookService adminBookService) {
		this.adminBookService = adminBookService;
	}
	
	public Bookinfo getBook() {
		return book;
	}

	public void setBook(Bookinfo book) {
		this.book = book;
	}

	public List<Bookinfo> getBooks() {
		return books;
	}

	public void setBooks(List<Bookinfo> books) {
		this.books = books;
	}

	public String toAdminBookList() throws Exception{
		int offset = 0;
		if(request.getParameter("pager.offset")==null){
			offset = 0;
		}
		else{
			offset = Integer.parseInt(request.getParameter("pager.offset"));
		}
		SPageBean spb = new SPageBean();
		spb.setOffset(offset);
		spb.setPageSize(4);
		//adminBookService.checkBook(spb);
		if(1==1){
			books = adminBookService.getAllBooksByPage(spb);
		}
		request.setAttribute("SPB", spb);
		//request.setAttribute("book", b);
		session.put("BOOK", books);
		//request.setAttribute("SPB", books);
		return "admin";
	}
	
	public String toStockBookList() throws Exception{
		int offset = 0;
		if(request.getParameter("pager.offset")==null){
			offset = 0;
		}
		else{
			offset = Integer.parseInt(request.getParameter("pager.offset"));
		}
		SPageBean spb = new SPageBean();
		spb.setOffset(offset);
		spb.setPageSize(4);
		books = adminBookService.getStockBooksByPage(spb);
		request.setAttribute("SPB", spb);
		return "stock";
	}
	
	public String toAdd() throws Exception{
		return "add";
	}
	public String doAdd() throws Exception{
		boolean flag = adminBookService.addBook(book);
		if(flag){
			response.sendRedirect("adminBook!toStockBookList.action");
			return null;
		}		
		return "add";
	}
	
	public String doDelete() throws Exception{
		response.setContentType("text/xml; charset=utf-8");
		PrintWriter out = response.getWriter();
		if(adminBookService.deleteBook(book.getBookid())){
			out.println("<result>ok</result>");
		}
		else{
			out.println("<result>xx</result>");
		}
		return null;
	}
	
	public String toQuery() throws Exception{
		int offset = 0;
		if(request.getParameter("pager.offset")==null){
			offset = 0;
		}
		else{
			offset = Integer.parseInt(request.getParameter("pager.offset"));
		}
		SPageBean spb = new SPageBean();
		System.out.println(offset);
		spb.setOffset(offset);
		spb.setPageSize(4);
		books = adminBookService.queryBook(spb, book);
		request.setAttribute("SPB", spb);	
		return "admin";
	}
	
	public String toHuan() throws Exception{
		book = adminBookService.getBookById(book.getBookid());
		book.setStatus(1);
		book.setStarttime(null);
		book.setUserinfo(null);
		if(adminBookService.updateBook(book)){
			response.sendRedirect("adminBook!toAdminBookList.action");
			return null;
		}
		response.sendRedirect("adminBook!toAdminBookList.action");
		return null;
	}
}
